package com.tbit.uqbike.webmanager.controller;

import com.tbit.common.constant.MethodEnum;
import com.tbit.common.constant.ModuleEnum;
import com.tbit.common.entity.view.Result;
import com.tbit.common.enums.FeeTypeEnum;
import com.tbit.common.enums.OperaPswEnum;
import com.tbit.uqbike.annos.OperateLog;
import com.tbit.uqbike.constant.TimeFeeConstant;
import com.tbit.uqbike.object.pojo.HolidayFeeDto;
import com.tbit.uqbike.object.pojo.ModifyFeeTypeDto;
import com.tbit.uqbike.object.pojo.TimeFeeVo;
import com.tbit.uqbike.object.value.TimeFee;
import com.tbit.uqbike.object.view.JsonResult;
import com.tbit.uqbike.service.business.OperaPswService;
import com.tbit.uqbike.service.business.TimeFeeService;
import com.tbit.uqbike.webmanager.util.Assert;
import com.tbit.uqbike.webmanager.util.ServletUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 分时计费规则
 *
 * @Author: chen
 * @Description:
 * @Date: 创建于 16:18 2023/11/2
 */
@RestController
@RequestMapping("/timeFee")
public class TimeFeeController {

    @Autowired
    private TimeFeeService timeFeeService;
    @Autowired
    private OperaPswService operaPswService;

    /**
     * 新增、修改
     *
     * @param timeFeeVo 计费实体类
     * @return JsonResult
     */
    @RequestMapping("/add")
    @OperateLog(module = ModuleEnum.ACCOUNTFEE, methods = MethodEnum.TIMEFEE_ADD)
    public JsonResult add(@RequestBody TimeFeeVo timeFeeVo) {
        Assert.notNull(timeFeeVo.getAccountId(),"ver.accountId.notNull");
        Assert.notNull(timeFeeVo.getType(),"timeFee.type.is.null");
        /**操作密码校验*/
        operaPswService.checkOperaPsw(BaseController.getLogin(ServletUtils.getRequest()),
                    OperaPswEnum.OPERA_PSW_SYSTEM_CONFIG.getModule(), timeFeeVo.getOperaPsw());

        if(TimeFeeConstant.TIMEFEE_MACHINEFEE.equals(timeFeeVo.getType())||TimeFeeConstant.TIMEFEE_VIPFEE.equals(timeFeeVo.getType())){
            Assert.notNull(timeFeeVo.getAttachId(),"timeFee.feeId.is.null");
        }
        timeFeeService.add(timeFeeVo);
        return JsonResult.ok();
    }

    /**
     * 通过区域id获取分时计费规则
     *
     * @param accountId 区域id
     * @param type      类型
     * @param attachId  附加id 车辆计费+会员卡计费需要传递此字段
     * @return JsonResult
     */
    @RequestMapping("/getByAccountId")
    public JsonResult getByAccountId(Integer accountId,@RequestParam(defaultValue = "0") Integer type,Integer attachId) {
        List<TimeFee> timeFees = timeFeeService.getByAccountId(accountId,type,attachId);
        return JsonResult.ok(timeFees);
    }

    /**
     * 添加假日计费规则
     * @param holidayFeeDto
     * @return
     */
    @PostMapping("/holidayAdd")
    @OperateLog(module = ModuleEnum.ACCOUNTFEE, methods = MethodEnum.HOLIDAYFEE_MODIFY)
    public Result<Void> addHolidayFee(@RequestBody HolidayFeeDto holidayFeeDto) {
        /**操作密码校验*/
        operaPswService.checkOperaPsw(BaseController.getLogin(ServletUtils.getRequest()),
                OperaPswEnum.OPERA_PSW_SYSTEM_CONFIG.getModule(), holidayFeeDto.getOperaPsw());
        timeFeeService.addHolidayFee(holidayFeeDto);
        return Result.success();
    }

    /**
     * 查询假日计费规则
     * @param accountId
     * @return
     */
    @GetMapping("/holidayQuery")
    public Result<List<TimeFee>> queryHoliday(Integer accountId){
        return Result.success(timeFeeService.queryHoliday(accountId));
    }

    /**
     * 查询当前区域生效的计费规则
     * @param accountId
     * @return
     */
    @GetMapping("/feeType")
    public Result<FeeTypeEnum> queryFeeType(Integer accountId){
        return Result.success(timeFeeService.queryFeeType(accountId));
    }

    /**
     * 维护计费规则开关
     * @param dto
     * @return
     */
    @PutMapping("/modifyFeeType")
    public Result<Void> modifyFeeType(@RequestBody ModifyFeeTypeDto dto){
        operaPswService.checkOperaPsw(BaseController.getLogin(ServletUtils.getRequest()),
                OperaPswEnum.OPERA_PSW_SYSTEM_CONFIG.getModule(), dto.getOperaPsw());
        timeFeeService.modifyFeeType(dto);
        return Result.success();
    }

}
